<%--
  Created by IntelliJ IDEA.
  User: SamZhao
  Date: 2016/12/6
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ include file="/WEB-INF/jsp/include.inc.jsp"%>


<!-- END JS -->


<!-- BEGIN SECTION -->
<section id="container" class="">

    <!-- BEGIN MAIN CONTENT -->
    <section id="main-content">
        <section class="wrapper site-min-height">
            <!-- BEGIN ROW  -->
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            <span class="label label-primary">借阅记录</span>
                            <span class="tools pull-right">
                           <a href="javascript:;" class="fa fa-chevron-down"></a>
                           <a href="javascript:;" class="fa fa-times"></a>
                           </span>
                        </header>
                        <div class="panel-body">
                            <div class="modal fade" id="myModal3" >
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title">确认!</h4>
                                        </div>
                                        <form class="cmxform form-horizontal tasi-form" id="commentForm" method="get">
                                            <input type="hidden" name="bookId" id="bookId"/>
                                            <input type="hidden" name="userId" id="userId"/>
                                            <input type="hidden" name="type" id="type"/>
                                            <input type="hidden" name="bookType" id="bookType" value="0"/>
                                            <div class="modal-body" id="erroText">
                                                请输入工号：<br/><br/>
                                                <input class="form-control " id="userNumber" type="text" name="userNumber">
                                            </div>
                                        </form>
                                        <div class="modal-footer">
                                            <button data-dismiss="modal" class="btn btn-default" type="button">取消</button>
                                            <button class="btn btn-warning" type="button" id="borrowConfirm" onclick="borrowConfirm();">确定</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal fade" id="myModal2" >
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title">确认!</h4>
                                        </div>
                                        <input type="hidden" name="deleteBookId" id="deleteBookId"/>
                                        <div class="modal-body">
                                            确定删除？
                                        </div>
                                        <div class="modal-footer">
                                            <button data-dismiss="modal" class="btn btn-default" type="button">取消</button>
                                            <button class="btn btn-warning" type="button" id="deleteConfirm" onclick="deleteConfirm();">确定</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal fade" id="info"  >
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title">提示</h4>
                                        </div>
                                        <div class="modal-body" id="infoText">
                                            提交成功!
                                        </div>
                                        <div class="modal-footer">
                                            <button class="btn btn-success" type="button" data-dismiss="modal">确定</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="adv-table">
                                <div class="space15"></div>

                                <table class="display table table-bordered table-striped" id="example">
                                    <thead>
                                    <tr>
                                        <th>书籍名称</th>
                                        <th>借阅类型</th>
                                        <th>借阅人</th>
                                        <th>借出日期</th>
                                        <th>归还日期</th>
                                        <th>借阅状态</th>
                                        <th>应还日期</th>
                                        <th style="width: 290px;">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                        <c:forEach items="${borlist }" var="item">
                                            <tr class="gradeX">
                                                <td>${item.bookName }</td>
                                                <td class="hidden-phone">
                                                    <c:if test="${item.borrowType == '1'}">报刊</c:if>
                                                    <c:if test="${item.borrowType == '0'}">书籍</c:if>
                                                </td>
                                                <td>${item.userName }</td>
                                                <td class="center hidden-phone"><fmt:formatDate value="${item.borrowOut }" pattern="yyyy-MM-dd" /></td>
                                                <td class="center hidden-phone"><fmt:formatDate value="${item.borrowBack }" pattern="yyyy-MM-dd" /></td>
                                                <td class="center hidden-phone">
                                                    <c:if test="${item.status == '1'}">已归还</c:if>
                                                    <c:if test="${item.status == '0'}">未归还</c:if>
                                                </td>
                                                <td class="center hidden-phone"><fmt:formatDate value="${item.borrowRequire }" pattern="yyyy-MM-dd" /></td>
                                                <td class="center hidden-phone">
                                                    <c:if test="${item.status == '0'}"><a class="edit" href="javascript:void(0);" onclick="borrowOut(${item.bookId },${item.borrowType}, 'back');"><span class="label label-success">归还</span></a></c:if>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
            <!-- END ROW  -->
        </section>
    </section>
    <!-- END SECTION -->

    <!-- BEGIN FOOTER -->
    <footer class="site-footer">
        <div class="text-center">
            2016 &copy; BMS4.0 by <a href="" target="_blank">SamZhao</a>.
            <a class="go-top">
                <i class="fa fa-angle-up"></i>
            </a>
        </div>
    </footer>
    <!-- END FOOTER  -->
</section>
<script type="text/javascript">

    function intervalRun(){
        $("#info").modal("hide");
        window.location = "/book/list.html";
    }

    function intervalRun1(){
        $("#myModal3").modal("hide");
        $("#myModal2").modal("hide");
        $("#info").modal("hide");
        window.location = "/borrow/list.html";
    }

    function showinfo(){
        $("#info").modal("hide");
    }

    function borrowOut(id,bookType,type){
        $("#bookId").val(id);
        $("#type").val(type);
        $("#bookType").val(bookType);
        $("#myModal3").modal("show");
    }

    function borrowConfirm(){
        if($("#userNumber").val() == ''){
            $("#infoText").html("请输入工号");
            $("#info").modal("show");
            window.setInterval(function()//延迟进行跳转
            {
                showinfo();
            }, 500);
        }else{
            var flag = checkUser();
            var type = $("#type").val();
            if(type == 'out' && flag == 0 ){
                $.ajax({
                    type: "POST",
                    url: "/borrow/saveBorrowOut.html",
                    data:$('#commentForm').serialize(),// 你的formid
                    async: false,
                    error: function() {
                        $("#infoText").html("网络异常!");
                        $("#info").modal("show");
                    },
                    success: function(data) {

                        if(data == '1'){
                            $("#infoText").html("借阅成功!");
                            $("#info").modal("show");

                            window.setInterval(function()//延迟进行跳转
                            {
                                intervalRun1();
                            }, 500);
                        }else{
                            $("#infoText").html("借阅出错!");
                            $("#info").modal("show");
                        }
                    }
                });
            }
            if(type == 'back' && flag == 0 ){
                $.ajax({
                    type: "POST",
                    url: "/borrow/saveBorrowBack.html",
                    data:$('#commentForm').serialize(),// 你的formid
                    async: false,
                    error: function() {
                        $("#infoText").html("网络异常!");
                        $("#info").modal("show");
                    },
                    success: function(data) {

                        if(data == '1'){
                            $("#infoText").html("归还成功!");
                            $("#info").modal("show");

                            window.setInterval(function()//延迟进行跳转
                            {
                                intervalRun1();
                            }, 500);
                        }else{
                            $("#infoText").html("归还出错!");
                            $("#info").modal("show");
                        }
                    }
                });
            }
        }
    }

    function checkUser(){
        var flag = 0;
        $.ajax({
            type: "POST",
            url: "/user/checkUserByNum.html?number="+$("#userNumber").val(),
            data:$('#commentForm').serialize(),// 你的formid
            async: false,
            error: function() {
                $("#infoText").html("网络异常!");
                $("#info").modal("show");
            },
            success: function(data) {
                if(data != 'null'){
                    $("#userId").val(data);
                }else{
                    $("#infoText").html("该工号不存在!");
                    $("#info").modal("show");
                    flag = 1;
                    return flag;
                }
            }
        });


        var type = $("#type").val();

        if(type == 'out' && flag == 0){
            $.ajax({
                type: "POST",
                url: "/borrow/checkBorrowOut.html",
                data:$('#commentForm').serialize(),// 你的formid
                async: false,
                error: function() {
                    $("#infoText").html("网络异常!");
                    $("#info").modal("show");
                },
                success: function(data) {
                    alert($.trim(data) == '0');
                    if(data == '0'){
                        $("#infoText").html("借阅成功!");
                        $("#info").modal("show");
                    }else if(data == '1'){
                        $("#infoText").html("书籍已全部借出!");
                        $("#info").modal("show");
                    }else if(data == '2'){
                        $("#infoText").html("已借阅该书籍!");
                        $("#info").modal("show");
                    }else{
                        $("#infoText").html("网络异常!");
                        $("#info").modal("show");
                    }
                }
            });
        }
        if(type == 'back' && flag == 0){
            $.ajax({
                type: "POST",
                url: "/borrow/checkBorrowBack.html",
                data:$('#commentForm').serialize(),// 你的formid
                async: false,
                error: function() {
                    $("#infoText").html("网络异常!");
                    $("#info").modal("show");
                },
                success: function(data) {
                    if(data == '0'){
                        $("#infoText").html("借阅成功!");
                        $("#info").modal("show");
                    }else if(data == '1'){
                        $("#infoText").html("未借阅该书籍!");
                        $("#info").modal("show");
                    }else{
                        $("#infoText").html("网络异常!");
                        $("#info").modal("show");
                    }
                }
            });
        }
        return flag;
    }

    $(function(){
        $("#myModal3").modal("hide");
        $("#myModal2").modal("hide");
        $("#info").modal("hide");
    });

</script>
